﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;
using XQ.SP.BllServices;
using XQ.SP.Model;
using XM.Comm;
using XM.PermissionFilter;

namespace XiaoQu.Controllers
{
    /// <summary>
    /// 文件临时存放
    /// </summary>
    [Description("文件临时存放")]
    public class BaseTempDataController : Controller
    {
        XQ.SP.BllServices.BaseTempDataBLL bllBase = new XQ.SP.BllServices.BaseTempDataBLL();

        #region 后台管理
        #region 列表
        [Description("列表")]
        [PermissionFilter]
        public ActionResult List(int pageIndex = 1)
        {
            string valueStr = Request.Form["search_input"];
            string search_type = Request.Form["search_type"];
            List<SelectListItem> ListSeach = new List<SelectListItem>();
            ListSeach.Add(new SelectListItem { Value = "imagespath", Text = "图片地址" });
            ListSeach.Add(new SelectListItem { Value = "imagesdesc", Text = "描述" });
            ViewBag.ListSeachType = ListSeach;
            @ViewBag.pageIndex=pageIndex;
            @ViewBag.ListSeachValue=valueStr;

            #region
            int pageSize = 20; 
            XQ.Model.Pager.Pager modelP = new XQ.Model.Pager.Pager();
            modelP.tableName = " BaseTempData ";
            modelP.fldName = " * ";
            modelP.pageSize = 20;
            modelP.page = pageIndex;
            modelP.fldSort = " Id ";
            modelP.sort = 1;
            modelP.id = " Id ";
            modelP.dist = 1;
            if (!string.IsNullOrEmpty(valueStr))
            {
                modelP.strCondition += " and "+search_type+" like '%" + valueStr + "%' ";
            }
            modelP = DBUtility.DbHelperSQL.GetPage(modelP);
            #endregion
            List<XQ.SP.Model.BaseTempDataItem> list = bllBase.DataTableToList(modelP.dt);
            return View(new PagedList<XQ.SP.Model.BaseTempDataItem>(list, pageIndex, pageSize, modelP.counts));
        }
        #endregion

        #region 编辑
        [Description("编辑")]
        [PermissionFilter]
        public ActionResult Edit(long id = 0,int tid=0)
        {
            BaseTempDataItem item = new BaseTempDataItem();
            item.Status = tid;
            if (id != 0)
            {
                item = bllBase.GetModel(id);
            }

            
            

            
            return View(item);
        }

        [Description("编辑")]
        [PermissionFilter]
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(BaseTempDataItem model)
        {
            
            
            
            
            #region
            try
            {
            string s="";
            if(model.ImagesPath.Length>4000)
            s="图片地址字符数不能多于4000 !";
            if(model.ImagesDesc.Length>200)
            s="描述字符数不能多于200 !";
            if(!string.IsNullOrEmpty(s))
            {
                ModelState.AddModelError("", s);
                return View(model);
            }
            }catch{}
            #endregion
            
            #region
            #endregion
            if (model.Id > 0)
            {
                var userCurr = UsersInfo.GetUserH();
                model.Modified = DateTime.Now;
                model.Modifier = userCurr.Id;
                if (bllBase.Update(model))
                    return RedirectToAction("List");
                else
                {
                    ModelState.AddModelError("", "编辑失败");
                }
            }
            else
            {
                var userCurr = UsersInfo.GetUserH();
                model.Creator = userCurr.Id;
                model.Created = DateTime.Now;
                model.Modified = DateTime.Now;
                if (bllBase.Add(model) > 0)
                    return RedirectToAction("List");
                else
                {
                    ModelState.AddModelError("", "添加失败");
                }
            }
            return View(model);
        }
        #endregion

        #region 删除
        [Description("删除")]
        [PermissionFilter]
        public JsonResult Deletes(string ids, long uid)
        {
            if (bllBase.DeleteList(ids))
                return Json("true", JsonRequestBehavior.AllowGet);

            return Json("false", JsonRequestBehavior.AllowGet);
        }
        #endregion
        #endregion

        #region 个人中心
        
        #region 个人中心列表
        [Description("个人中心列表")]
        [PermissionFilter]
        public ActionResult MyList(int pageIndex = 1)
        {
            string valueStr = Request.Form["search_input"];
            string search_type = Request.Form["search_type"];
            List<SelectListItem> ListSeach = new List<SelectListItem>();
            ListSeach.Add(new SelectListItem { Value = "imagespath", Text = "图片地址" });
            ListSeach.Add(new SelectListItem { Value = "imagesdesc", Text = "描述" });
            ViewBag.ListSeachType = ListSeach;
            @ViewBag.pageIndex=pageIndex;
            @ViewBag.ListSeachValue=valueStr;

            #region
            int pageSize = 20; 
            XQ.Model.Pager.Pager modelP = new XQ.Model.Pager.Pager();
            modelP.tableName = " BaseTempData ";
            modelP.fldName = " * ";
            modelP.pageSize = 20;
            modelP.page = pageIndex;
            modelP.fldSort = " Id ";
            modelP.sort = 1;
            modelP.id = " Id ";
            modelP.dist = 1;
            if (!string.IsNullOrEmpty(valueStr))
            {
                modelP.strCondition += " and "+search_type+" like '%" + valueStr + "%' ";
            }
            modelP.strCondition += " and Creator ='" + UsersInfo.GetUsersID() + "' ";
            modelP = DBUtility.DbHelperSQL.GetPage(modelP);
            #endregion
            List<XQ.SP.Model.BaseTempDataItem> list = bllBase.DataTableToList(modelP.dt);
            return View(new PagedList<XQ.SP.Model.BaseTempDataItem>(list, pageIndex, pageSize, modelP.counts));
        }
        #endregion

        #region 个人中心编辑
        [Description("个人中心编辑")]
        [PermissionFilter]
        public ActionResult MyEdit(long id = 0,int tid=0)
        {
            BaseTempDataItem item = new BaseTempDataItem();
            item.Status = tid;
            if (id != 0)
            {
                item = bllBase.GetModel(id);
            }

            
            
            
            return View(item);
        }

        [Description("个人中心编辑")]
        [PermissionFilter]
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult MyEdit(BaseTempDataItem model)
        {
            
            
            
            
            #region
            try
            {
            string s="";
            if(model.ImagesPath.Length>4000)
            s="图片地址字符数不能多于4000 !";
            if(model.ImagesDesc.Length>200)
            s="描述字符数不能多于200 !";
            if(!string.IsNullOrEmpty(s))
            {
                ModelState.AddModelError("", s);
                return View(model);
            }
            }catch{}
            #endregion
            
            #region
            #endregion
            if (model.Id > 0)
            {
                model.Modified = DateTime.Now;
                model.Modifier = UsersInfo.GetUser().Id;
                if (bllBase.Update(model))
                    return RedirectToAction("MyList");
                else
                {
                    ModelState.AddModelError("", "编辑失败");
                }
            }
            else
            {
                model.Creator = UsersInfo.GetUser().Id;
                model.Created = DateTime.Now;
                model.Modified = DateTime.Now;
                if (bllBase.Add(model) > 0)
                    return RedirectToAction("MyList");
                else
                {
                    ModelState.AddModelError("", "添加失败");
                }
            }
            return View(model);
        }
        #endregion

        #region 个人中心删除
        [Description("个人中心删除")]
        [PermissionFilter]
        public ActionResult MyDelete(long id, int pageIndex)
        {
            var item= bllBase.GetModel(id);

            if (bllBase.Delete(id))
                    return RedirectToAction("MyList", new { pageIndex = pageIndex });
                else
                    return RedirectToAction("MyList");
        }
        #endregion
        #endregion

        #region PC端页面
        #region 查询页面
        [Description("查询页面")]
        [PermissionFilter]
        public ActionResult Sele(int pageIndex = 1)
        {
            string valueStr = Request.Form["search_input"];
            string search_type = Request.Form["search_type"];
            List<SelectListItem> ListSeach = new List<SelectListItem>();
            ListSeach.Add(new SelectListItem { Value = "name", Text = "标题" });
            ListSeach.Add(new SelectListItem { Value = "keys", Text = "关键字" });
            ViewBag.ListSeachType = ListSeach;
            @ViewBag.pageIndex = pageIndex;
            @ViewBag.ListSeachValue = valueStr;

            #region
            int pageSize = 20;
            XQ.Model.Pager.Pager modelP = new XQ.Model.Pager.Pager();
            modelP.tableName = " BaseTempData ";
            modelP.fldName = " * ";
            modelP.pageSize = 20;
            modelP.page = pageIndex;
            modelP.fldSort = " Id ";
            modelP.sort = 1;
            modelP.id = " Id ";
            modelP.dist = 1;
            if (!string.IsNullOrEmpty(valueStr))
            {
                modelP.strCondition += " and " + search_type + " like '%" + valueStr + "%' ";
            }
            modelP = DBUtility.DbHelperSQL.GetPage(modelP);
            #endregion
            List<XQ.SP.Model.BaseTempDataItem> list = bllBase.DataTableToList(modelP.dt);

            return View(new PagedList<XQ.SP.Model.BaseTempDataItem>(list, pageIndex, pageSize, modelP.counts));
        }
        #endregion
        #region 详细
        [Description("详细页面")]
        [PermissionFilter]
        public ActionResult D(long id = 1)
        {
            var item = bllBase.GetModel(id);
            return View(item);
        }
        #endregion
        #endregion

        #region APP 小程序 对外接口
        #region 查询接口
        [Description("实体列表接口")]
        [PermissionFilter]
        public JsonResult Selei(int pageIndex = 1,Int64 Id = 0 ,Int64 UserId = 0 ,Int64 SysTap = 0 ,Int64 FilesTap = 0 ,String ImagesPath = "",String ImagesDesc = "",Int32 Status = 0 ,Int64 Creator = 0 ,String Created = "",Int64 Modifier = 0 ,String Modified = "")
        {
            string valueStr = Request.Form["search_input"];
            string search_type = Request.Form["search_type"];
            List<SelectListItem> ListSeach = new List<SelectListItem>();
            ListSeach.Add(new SelectListItem { Value = "name", Text = "标题" });
            ListSeach.Add(new SelectListItem { Value = "keys", Text = "关键字" });
            ViewBag.ListSeachType = ListSeach;
            @ViewBag.pageIndex = pageIndex;
            @ViewBag.ListSeachValue = valueStr;

            #region
            int pageSize = 20;
            XQ.Model.Pager.Pager modelP = new XQ.Model.Pager.Pager();
            modelP.tableName = " BaseTempData ";
            modelP.fldName = " * ";
            modelP.pageSize = pageSize;
            modelP.page = pageIndex;
            modelP.fldSort = " Id ";
            modelP.sort = 1;
            modelP.id = " Id ";
            modelP.dist = 1;
            if (!string.IsNullOrEmpty(valueStr))
            {
                modelP.strCondition += " and " + search_type + " like '%" + valueStr + "%' ";
            }
            
            if (Id!=0)
            {
                modelP.strCondition += " and Id ="+Id+" ";
            }
            if (UserId!=0)
            {
                modelP.strCondition += " and UserId ="+UserId+" ";
            }
            if (SysTap!=0)
            {
                modelP.strCondition += " and SysTap ="+SysTap+" ";
            }
            if (FilesTap!=0)
            {
                modelP.strCondition += " and FilesTap ="+FilesTap+" ";
            }
            if (!string.IsNullOrEmpty(ImagesPath))
            {
                modelP.strCondition += " and ImagesPath like '%"+ImagesPath+"%' ";
            }
            if (!string.IsNullOrEmpty(ImagesDesc))
            {
                modelP.strCondition += " and ImagesDesc like '%"+ImagesDesc+"%' ";
            }
            if (Status!=0)
            {
                modelP.strCondition += " and Status ="+Status+" ";
            }
            if (Creator!=0)
            {
                modelP.strCondition += " and Creator ="+Creator+" ";
            }
            if (!string.IsNullOrEmpty(Created))
            {
                modelP.strCondition += " and Created like '%"+Created+"%' ";
            }
            if (Modifier!=0)
            {
                modelP.strCondition += " and Modifier ="+Modifier+" ";
            }
            if (!string.IsNullOrEmpty(Modified))
            {
                modelP.strCondition += " and Modified like '%"+Modified+"%' ";
            }
            modelP = DBUtility.DbHelperSQL.GetPage(modelP);
            #endregion
            List<XQ.SP.Model.BaseTempDataItem> list = bllBase.DataTableToList(modelP.dt);

            return Json(list, JsonRequestBehavior.AllowGet);
            //return View(new PagedList<XQ.SP.Model.BaseTempDataItem>(list, pageNo, pageSize, pager.counts));
        }
        #endregion
        #region 单个实体接口
        [Description("单个实体接口")]
        [PermissionFilter]
        public JsonResult Di(long id = 0)
        {
            var item = bllBase.GetModel(id);
            return Json(item, JsonRequestBehavior.AllowGet);
        }
        #endregion
        #endregion
    }
}

